


package modules::misc;

use module;
use vars '@ISA';
@ISA = ("module");
use DBI;
use encoding "utf8";
use utf8;
use Time::Interval;

my $innah = "Иннах..";

sub save {
    my ($pkg,$writer) = @_;
    $writer->emptyTag("blah",yo => 'hehe');
}


sub get_time {
    my ($pkg,%args) = @_;

    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);
    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
	localtime(time());
    $min += 1;
    $year += 1900;
    main::SendMessage( to   => $args{from},
		       body => "it's $hour:$min"
		       );
}

sub get_help {
    my ($pkg,%args) = @_;

    my $msg = qq~
Вся информация на
http://forum.ipse.ru
    ~;
 main::SendMessage( to => $args{from}, body => $msg );

}

sub got_invitation {
    my ($pkg,%args) = @_;

my $blacklist = "/home/ipse/perlbot/jpb-0.3.3/blackjid";
open (BLACK, $blacklist);
@blackjidlist=<BLACK>;
$controljid = "aa";
foreach (@blackjidlist) {
$ctlj=$args{other}->{jid};
if (/$ctlj/) {$controljid = ""};
}

close BLACK;

    if($controljid ne "") {
      main::pd("Got invitation for $args{other}->{jid}");
      main::SendPresence(to => $args{other}->{jid} . "/$::nickname",
			 status => "Ruff!");
    }
    else { main::pd("HELP !!! $args{other}->{jid}"); }
$controljid = "aa";
}

sub got_groupchat_time {
    my ($pkg,%args) = @_;

    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);
    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
	localtime(time());
    $min += 1;
    $year += 1900;
if ($min<=9) {
$min = "0.$min";
}
    main::SendMessage( to   => $args{from}, type => 'groupchat',
		       body => "it's $hour:$min"
		       );
#main::SendMessage(to => $args{from}, type => 'groupchat', body => "JOJO");
}

sub got_groupchat_man {
    my ($pkg,%args) = @_;
    my (@words_man) = split(/ /,$args{body});
if ($words_man[1] =~ /^\w+$/) {
open (MAN, "man -P cat $words_man[1] | ") || &ashipka;
@man =  <MAN> || &ashipka;
close MAN;
} else {
@manresult = "$args{resource}: хакир мля..";
}
main::SendMessage(to => $args{from}, type => 'groupchat', body => "$man[0] @manresult");
}

sub ashipka {
my ($pkg,%args) = @_;
main::SendMessage(to => $args{from}, type => 'groupchat', body => "Ашыпка! \n$!");
}

sub got_groupchat_ohtung {
    my ($pkg,%args) = @_;

main::SendMessage(to => $args{from}, type => 'groupchat', body => "ОХТУНГ!!!");
}

sub got_groupchat_uptime {
    my $b;
    my ($pkg,%args) = @_;
$tekuschee_vremya = time;
$proshlo_vremeni = ($tekuschee_vremya - $'nachalo_raboty);

$template = parseInterval(
                seconds => $proshlo_vremeni,
                String  => 1
        );

$template =~ /(\d) (second?)/;
$b=time_oko($1);
$template =~ s/seconds?/секунд$b/;
$template =~ /(\d) (minutes?)/;
$b=time_oko($1);
$template =~ s/minutes?/минут$b/;
$template =~ /(\d) (hours?)/;
$b=time_oko($1);
$template =~ s/hours?/часов/;
$template =~ /(\d) (days?)/;
$b=time_oko($1);
$template =~ s/days?/дней/;

main::SendMessage(to => $args{from}, type => 'groupchat', body => "Время моей бесперерывной работы $template");
}

sub got_groupchat_downloads {
    my ($pkg,%args) = @_;
my $dird = "/home/ipse/.dc++/Downloads";
opendir (DIR, $dird);
@dirlst = readdir(DIR);
foreach(@dirlst)
{
if(/^\./)
{
$_=undef;
}else{
push (@listd, "$_\n");
}
}
close DIR;
main::SendMessage(to => $args{from}, type => 'groupchat', body => "\n@listd");
@listd=undef;
}
sub got_groupchat_incomplete {
    my ($pkg,%args) = @_;

$dir = "/home/ipse/.dc++/Incomplete";
#opendir(DIR, $dir );

open (DIR2, "ls -l $dir | ");
@list =  <DIR2>;
foreach(@list )
{
if(/^t/)
{
$_=undef;
}else{
@razmer = split;
$temp =  "Size $razmer[4] bytes, Time $razmer[7], File $razmer[8]\n";
push (@spisok, $temp);
}
}
close DIR2;
$_ = undef;

$dir2="/usr/home/ipse/Desktop";
open (MOVEZ, "ls -l \"$dir2\" | ");
@list2=<MOVEZ>;
foreach(@list2)
{
if(/.*part$/)
{
@razmer = split;
$temp2 =  "Size $razmer[4] bytes, Time $razmer[7], File $razmer[8]\n";
push (@spisok, $temp2);
}
}
$_=undef;
close MOVEZ;

main::SendMessage(to => $args{from}, type => 'groupchat', body => "\n@spisok");
@spisok=undef;

}

sub got_groupchat_percent {
    my ($pkg,%args) = @_;

my $queue_file = "/home/ipse/.dc++/Queue.xml";
open (QUE, $queue_file);
@queue_file = <QUE>;
foreach (@queue_file)
 {
 if (/Download Target/)
  {
  @stroka=split(/[\/\"]/);
  $stroka[6] =~ s/&apos;/'/g;
  $percent = ($stroka[23]/$stroka[8])*100;
  $percent =~ s/(\d*\.\d\d)(.*)/$1/;
  $percent = "$stroka[6] $percent\%\n";
  utf8::decode($percent);
  push (@spisok_percent, $percent);
  }
 }
close QUE;
main::SendMessage(to => $args{from}, type => 'groupchat', body => "\n@spisok_percent");
@spisok_percent=undef;

}
sub got_anek {
    my ($pkg,%args) = @_;


$dbh = DBI->connect("DBI:mysql:$::db_anek:$::db_host:$::db_port", $::db_user, $::db_password);
$dbh->do("SET character_set_results='utf8'");
if ($sth_rv != 1) {
$sql = "SELECT
        Id,
        text
        FROM
        raznoe";
} else {
$sql = "SELECT
        Id,
        text
        FROM
        raznoe
        WHERE
        Id='$random'";
}

$sth = $dbh->prepare($sql);
$sth->execute();
if ($sth_rv != 1) {
$rv = $sth->rows;
$sth_rv = 1;
}
$sth->bind_columns(\$Id, \$anekdot);
$random = int(rand($rv));
$sth->fetch();

utf8::decode($anekdot);

main::SendMessage(to => $args{from}, type => $args{type}, body => "$anekdot");
$sth->finish;
$dbh->disconnect();
}


sub got_groupchat_anek_search_debug {
    my ($pkg,%args) = @_;
    my (@words) = split(/ /,$args{body});
    shift @words;
    foreach (@words) {
    $words .= "$_ ";
    }
    chop $words;

    if ($#words > 0) {
    $slov22 = "словами";
    } else {
    $slov22 = "словом";
    }
$words =~ s/[\*"()\$\[\]\|\+\'\\\/%]//g;
#main::SendMessage(to => $args{from}, type => 'groupchat',body => "Розыскиваю боянъ со $slov22 \"$words\""); # debug
$dbh = DBI->connect("DBI:mysql:$::db_anek:$::db_host:$::db_port", $::db_user, $::db_password);
$dbh->do("SET character_set_results='utf8'");
$sql = "SELECT
        Id,
        text
        FROM
        raznoe";

$sth = $dbh->prepare($sql);
$sth->execute();
$sth->bind_columns(\$Id, \$anekdot);

while ($sth->fetch())
{
utf8::decode($anekdot);
$_ = $anekdot;
if (/($words)/) {
push (@boyan_slovo, $anekdot);
$count2++;
}
}
$count44 = @boyan_slovo; # 6 октября 2007
$random2 = int(rand($count44));
$random2--;
if (!$count2) {
main::SendMessage(to => $args{from}, type => 'groupchat',body => "Неудачнег");
} else {
main::SendMessage(to => $args{from}, type => 'groupchat',body => "$boyan_slovo[$random2-1]");
main::SendMessage(to => $args{from}, type => 'groupchat',body => "Всего боянов со $slov22 \"$words\" $count2");
@boyan_slovo = undef;
}
$_ = undef;
$sth->finish;
$words = undef;
@words = undef;
$count2 = 0;


}

sub got_groupchat_anek_search {
    my ($pkg,%args) = @_;
    my (@words) = split(/ /,$args{body});
    shift @words;
if ($words[0] ne "база") {
    foreach (@words) {
    $words .= "$_ ";
    }
    chop $words;
    if ($#words > 0) {
    $slov22 = "словами";
    } else {
    $slov22 = "словом";
    }
#utf8::encode($words);
#main::SendMessage(to => $args{from}, type => 'groupchat',body => "Розыскиваю боянъ со $slov22 \"$words\""); # debug
$dbh = DBI->connect("DBI:mysql:$::db_anek:$::db_host:$::db_port", $::db_user, $::db_password);
#$dbh->do("SET character_set_results='utf8'");
$dbh->do("SET NAMES utf8");
$sql = "SELECT
        Id,
        text
        FROM
        raznoe
        WHERE
        text LIKE '%$words%'";
$sth = $dbh->prepare($sql);
$sth->execute();
$sth->bind_columns(\$Id, \$anekdot);

if ($sth->rows() > 0) {
pop @boyan_slovo;
while ($sth->fetch()) {
utf8::decode($anekdot);
push (@boyan_slovo, $anekdot);
$count2++;
}

$random2 = int(rand($#boyan_slovo));

main::SendMessage(to => $args{from}, type => 'groupchat',body => "$boyan_slovo[$random2]");
main::SendMessage(to => $args{from}, type => 'groupchat',body => "Всего боянов со $slov22 \"$words\" $count2");
} else {
main::SendMessage(to => $args{from}, type => 'groupchat',body => "Неудачнег");
}
$#boyan_slovo = 0;
$_ = undef;
$sth->finish;
$words = undef;
@words = undef;
$count2 = 0;
} else {
main::SendMessage(to => $args{from}, type => 'groupchat', body => "Анекдотов в базе: $rv");
}
}

sub got_groupchat_bse {
    my ($pkg,%args) = @_;
    my (@words) = split(/ /,$args{body});
    shift @words;
    foreach (@words) {
    $words .= "$_ ";
    }
    chop $words;
    if ($#words > 0) {
    $slov22 = "слов";
    } else {
    $slov22 = "слова";
    }
#utf8::encode($words);
main::SendMessage(to => $args{from}, type => 'groupchat',body => "Розыскиваю значение $slov22 \"$words\""); # debug
$dbh = DBI->connect("DBI:mysql:$::db_bse:$::db_host:$::db_port", $::db_user, $::db_password);
$dbh->do("SET NAMES utf8");
if ($words =~ /^".*"$/) {
$words =~ s/^"(.*)"$/$1/;
$sth = $dbh->prepare("SELECT id, slovo, znachenie FROM vse WHERE slovo LIKE '$words'");
} else {
$sth = $dbh->prepare("SELECT id, slovo, znachenie FROM vse WHERE slovo LIKE '%$words%'");
}
$sth->execute();
$sth->bind_columns(\$id5, \$slovo, \$znachenie);
if ($sth->rows() > 0) {
pop @bse_slovo;
pop @bse_znachenie;
while ($sth->fetch()) {
utf8::decode($slovo);
utf8::decode($znachenie);
push (@bse_slovo, $slovo);
push (@bse_znachenie, $znachenie);
$count4++;
}
#$count55 = $#bse_slovo;
#$count55++;
$random5 = int(rand($#bse_slovo));
#$random5--;
main::SendMessage(to => $args{from}, type => 'groupchat',body => "$bse_slovo[$random5] $bse_znachenie[$random5]");
main::SendMessage(to => $args{from}, type => 'groupchat',body => "Всего значений $slov22 \"$words\" $count4");
} else {
main::SendMessage(to => $args{from}, type => 'groupchat',body => "Неудачнег");
}
$#bse_slovo = 0;
$#bse_znachenie = 0;
$_ = undef;
$sth->finish;
$words = undef;
@words = undef;
$count4 = 0;
}

sub time_oko {
my ($a, $b) = @_;
$b=""if $a =~ /0|5..9/;
$b="ы"if $a =~ /[234]/;
$b="а"if $a =~ /1/;
return $b;
}

main::pd("................\n\n\n");
1;
